home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume13 / bool-eval < prev    next >
Encoding:
Internet Message Format  |  1988-01-31  |  55.4 KB

  1. Subject:  v13i047:  Boolean expression array evaluator
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: jim frost <madd@bucsb.bu.edu>
  7. Posting-number: Volume 13, Issue 47
  8. Archive-name: bool-eval
  9.  
  10. [  Change the calls to the cget() macro to use int, rather than char.
  11.    See build() in build.c  --r$  ]
  12.  
  13. Enclosed is the source to a program that I wrote called "bool".  It is
  14. a boolean expression array evaluator and is somewhat useful in
  15. designing PALs (programmed array logic chips) and also if you're an
  16. engineering student who has to develop truth tables for arrays of
  17. functions.  A complete description is enclosed in the file bool.1,
  18. which is a standard format man page.
  19.  
  20. I have linted the source and it appears to be as clean as it can get
  21. (only real error is that EOF is defined as -1 and it's compared to a
  22. char -- lint says "nonportable" but we've ported it to a variety of
  23. machines with no problem and I can't really fix it anyway since it's a
  24. definition in <stdio.h>).
  25.  
  26. Sample boolean files are also enclosed.  The whole deal has been
  27. pretty well tested (even though the version numbers are recent we have
  28. dumped a variety of actual tests through it and have not had a mistake
  29. since the last revision).
  30.  
  31. Cheers,
  32.  
  33. jim frost
  34. madd@bucsb.bu.edu
  35. ..!harvard!bu-cs!madd
  36.  
  37. -- cut here --
  38. #! /bin/sh
  39. # This is a shell archive.  Remove anything before this line, then unpack
  40. # it by saving it into a file and typing "sh file".  To overwrite existing
  41. # files, type "sh file -c".  You can also feed this as standard input via
  42. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  43. # will see the following message at the end:
  44. #        "End of shell archive."
  45. # Contents:  Copyright Makefile alloc.c bool.1 bool.c bool.h build.c
  46. #   eval_file.c eval_func.c misc.c sample1.b sample2.b sample3.b
  47. #   sample4.b sample5.b token.c
  48.  
  49. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  50. if test -f Copyright -a "${1}" != "-c" ; then 
  51.   echo shar: Will not over-write existing file \"Copyright\"
  52. else
  53. echo shar: Extracting \"Copyright\" \(2062 characters\)
  54. sed "s/^X//" >Copyright <<'END_OF_Copyright'
  55. X/* (c) copyright 1987 jim frost
  56. X * all rights reserved
  57. X *
  58. X * this program is copyrighted material.  the author gives permission
  59. X * to duplicate and redistribute this program provided the following
  60. X * conditions are met:
  61. X *   - this copyright notice is not removed.
  62. X *   - all duplicate copies or distributions contain full source
  63. X *     and documentation, including copyright notices.
  64. X *   - duplicate copies or distributions outside a single site are
  65. X *     original distributions without modifications.  (this is to keep
  66. X *     bastardized versions from showing up all over thie place.)
  67. X *
  68. X * this program source may be modified provided the following
  69. X * conditions are met:
  70. X *   - modified source is not distributed to other sites.
  71. X *   - modifications (including, but not limited to, bug fixes) are
  72. X *     sent to the author if the modifications are to be distributed.
  73. X *     no modified source is to be distributed unless done so by the
  74. X *     author.
  75. X *
  76. X * no warranty, either express or implied, is given for this program.
  77. X * the author makes no guarantees of fitness for any use of this
  78. X * program.  the author is not responsible for damages resulting from
  79. X * the use of this program for any purpose.
  80. X *
  81. X * 'site' refers to one or more computers under a single management.
  82. X * 'author' refers to the copyright holder, jim frost.
  83. X * 'source' refers to all files related to this program.
  84. X * 'documentation' refers to non-compilable files in the distribution.
  85. X *
  86. X * basically this notice is to keep me out of trouble should anything
  87. X * go wrong (i really *do* test these things though) and to make sure
  88. X * that the distribution of code is centralized.  makes bug fixes and
  89. X * enhancements much easier.
  90. X *
  91. X * thank you for your attention to this copyright notice.  if everyone
  92. X * follows this, you may find this a useful tool that is pretty well
  93. X * supported.
  94. X *
  95. X * author information:
  96. X *   jim frost                    permanent usnail address:
  97. X *   madd@bucsb.bu.edu            75 washington street
  98. X *   ..!harvard!bu-cs!bucsb!madd  laconia, nh  03246.
  99. X */
  100. END_OF_Copyright
  101. if test 2062 -ne `wc -c <Copyright`; then
  102.     echo shar: \"Copyright\" unpacked with wrong size!
  103. fi
  104. # end of overwriting check
  105. fi
  106. if test -f Makefile -a "${1}" != "-c" ; then 
  107.   echo shar: Will not over-write existing file \"Makefile\"
  108. else
  109. echo shar: Extracting \"Makefile\" \(2426 characters\)
  110. sed "s/^X//" >Makefile <<'END_OF_Makefile'
  111. X# (c) copyright 1987 jim frost
  112. X# all rights reserved
  113. X#
  114. X# this program is copyrighted material.  the author gives permission
  115. X# to duplicate and redistribute this program provided the following
  116. X# conditions are met:
  117. X#   - this copyright notice is not removed.
  118. X#   - all duplicate copies or distributions contain full source
  119. X#     and documentation, including copyright notices.
  120. X#   - duplicate copies or distributions outside a single site are
  121. X#     original distributions without modifications.  (this is to keep
  122. X#     bastardized versions from showing up all over thie place.)
  123. X#
  124. X# this program source may be modified provided the following
  125. X# conditions are met:
  126. X#   - modified source is not distributed to other sites.
  127. X#   - modifications (including, but not limited to, bug fixes) are
  128. X#     sent to the author if the modifications are to be distributed.
  129. X#     no modified source is to be distributed unless done so by the
  130. X#     author.
  131. X#
  132. X# no warranty, either express or implied, is given for this program.
  133. X# the author makes no guarantees of fitness for any use of this
  134. X# program.  the author is not responsible for damages resulting from
  135. X# the use of this program for any purpose.
  136. X#
  137. X# 'site' refers to one or more computers under a single management.
  138. X# 'author' refers to the copyright holder, jim frost.
  139. X# 'source' refers to all files related to this program.
  140. X# 'documentation' refers to non-compilable files in the distribution.
  141. X#
  142. X# basically this notice is to keep me out of trouble should anything
  143. X# go wrong (i really *do* test these things though) and to make sure
  144. X# that the distribution of code is centralized.  makes bug fixes and
  145. X# enhancements much easier.
  146. X#
  147. X# thank you for your attention to this copyright notice.  if everyone
  148. X# follows this, you may find this a useful tool that is pretty well
  149. X# supported.
  150. X#
  151. X# author information:
  152. X#   jim frost                    permanent usnail address:
  153. X#   madd@bucsb.bu.edu            75 washington street
  154. X#   ..!harvard!bu-cs!bucsb!madd  laconia, nh  03246.
  155. X#
  156. XCFLAGS= -O
  157. X
  158. Xall:        bool
  159. X
  160. Xbool:        alloc.o bool.o build.o eval_file.o eval_func.o misc.o token.o
  161. X        cc $(CFLAGS) -o bool alloc.o bool.o build.o eval_file.o eval_func.o misc.o token.o
  162. X
  163. Xclean:
  164. X        rm -f *.o
  165. X
  166. X*.c.o:
  167. X        cc -c $(CFLAGS) $*
  168. X
  169. Xalloc.o:    bool.h alloc.c
  170. Xbool.o:        bool.h bool.c
  171. Xbuild.o:    bool.h build.c
  172. Xeval_file.o:    bool.h eval_file.c
  173. Xeval_func.o:    bool.h eval_func.c
  174. Xmisc.o:        bool.h misc.c
  175. Xtoken.o:    bool.h token.c
  176. END_OF_Makefile
  177. if test 2426 -ne `wc -c <Makefile`; then
  178.     echo shar: \"Makefile\" unpacked with wrong size!
  179. fi
  180. # end of overwriting check
  181. fi
  182. if test -f alloc.c -a "${1}" != "-c" ; then 
  183.   echo shar: Will not over-write existing file \"alloc.c\"
  184. else
  185. echo shar: Extracting \"alloc.c\" \(3725 characters\)
  186. sed "s/^X//" >alloc.c <<'END_OF_alloc.c'
  187. X/* (c) copyright 1987 jim frost
  188. X * all rights reserved
  189. X *
  190. X * this program is copyrighted material.  the author gives permission
  191. X * to duplicate and redistribute this program provided the following
  192. X * conditions are met:
  193. X *   - this copyright notice is not removed.
  194. X *   - all duplicate copies or distributions contain full source
  195. X *     and documentation, including copyright notices.
  196. X *   - duplicate copies or distributions outside a single site are
  197. X *     original distributions without modifications.  (this is to keep
  198. X *     bastardized versions from showing up all over thie place.)
  199. X *
  200. X * this program source may be modified provided the following
  201. X * conditions are met:
  202. X *   - modified source is not distributed to other sites.
  203. X *   - modifications (including, but not limited to, bug fixes) are
  204. X *     sent to the author if the modifications are to be distributed.
  205. X *     no modified source is to be distributed unless done so by the
  206. X *     author.
  207. X *
  208. X * no warranty, either express or implied, is given for this program.
  209. X * the author makes no guarantees of fitness for any use of this
  210. X * program.  the author is not responsible for damages resulting from
  211. X * the use of this program for any purpose.
  212. X *
  213. X * 'site' refers to one or more computers under a single management.
  214. X * 'author' refers to the copyright holder, jim frost.
  215. X * 'source' refers to all files related to this program.
  216. X * 'documentation' refers to non-compilable files in the distribution.
  217. X *
  218. X * basically this notice is to keep me out of trouble should anything
  219. X * go wrong (i really *do* test these things though) and to make sure
  220. X * that the distribution of code is centralized.  makes bug fixes and
  221. X * enhancements much easier.
  222. X *
  223. X * thank you for your attention to this copyright notice.  if everyone
  224. X * follows this, you may find this a useful tool that is pretty well
  225. X * supported.
  226. X *
  227. X * author information:
  228. X *   jim frost                    permanent usnail address:
  229. X *   madd@bucsb.bu.edu            75 washington street
  230. X *   ..!harvard!bu-cs!bucsb!madd  laconia, nh  03246.
  231. X */
  232. X/* alloc.c:
  233. X *
  234. X * this file contains memory allocation and freeing routines
  235. X */
  236. X
  237. X#include "bool.h"
  238. X
  239. Xextern FUNC_LIST  *f_list;
  240. Xextern TOKEN_LIST *t_list;
  241. Xextern int        max_token,
  242. X                  err;
  243. X
  244. XFUNC_LIST *new_func(name)
  245. Xchar *name;
  246. X{ FUNC_LIST *f,*g;
  247. X
  248. X  if ((f= (FUNC_LIST *)malloc(sizeof(FUNC_LIST))) == NULL) {
  249. X    perror("\nMemory allocation error");
  250. X    exit(1);
  251. X  }
  252. X  if (!f_list)
  253. X    f_list= f;
  254. X  else {        /* append to list and check for duplicates */
  255. X    if (!strcmp(name,f_list->func)) {
  256. X      printf("\nDuplicate function name.\n");
  257. X      exit(1);
  258. X    }
  259. X    g= f_list;
  260. X    while (g->next) {
  261. X      if (!strcmp(name,g->func)) {
  262. X        printf("\nDuplicate function name.\n");
  263. X        exit(1);
  264. X      }
  265. X      g= g->next;
  266. X    }
  267. X    g->next= f;
  268. X  }
  269. X  strcpy(f->func,name);
  270. X  f->bool_exp= NULL;
  271. X  f->next= NULL;
  272. X  return(f);
  273. X}
  274. X
  275. X/*
  276. X * this function creates a new token
  277. X */
  278. X
  279. XTOKEN_LIST *new_token(s)
  280. Xchar *s;
  281. X{ TOKEN_LIST *t;
  282. X
  283. X  if ((t= (TOKEN_LIST *)malloc(sizeof(TOKEN_LIST))) == NULL) {
  284. X    perror("\nMemory allocation error");
  285. X    exit(1);
  286. X  }
  287. X  strcpy(t->token,s);
  288. X  t->number= max_token++;
  289. X  t->next= NULL;
  290. X  return(t);
  291. X}
  292. X
  293. X/*
  294. X * this function creates a new BOOL_EXP node
  295. X */
  296. X
  297. XBOOL_EXP *newbnode()
  298. X{ BOOL_EXP *t;
  299. X
  300. X    if ((t= (BOOL_EXP *)malloc(sizeof(BOOL_EXP))) == NULL) {
  301. X      err= 1;
  302. X      perror("\nMemory allocation error");
  303. X      return(NULL);
  304. X    }
  305. X    t->opcode= UNDEF;
  306. X    t->value= UNDEF;
  307. X    t->b1=
  308. X    t->b2= NULL;
  309. X    t->n1=
  310. X    t->n2= 0;
  311. X    return(t);
  312. X}
  313. X
  314. X/*
  315. X * this function free()s up a boolean tree.
  316. X */
  317. X
  318. Xvoid free_tree(b)
  319. XBOOL_EXP *b;
  320. X{
  321. X  if (!b)
  322. X    return;
  323. X  free_tree(b->b1);
  324. X  free_tree(b->b2);
  325. X  free((char *)b);
  326. X}
  327. END_OF_alloc.c
  328. if test 3725 -ne `wc -c <alloc.c`; then
  329.     echo shar: \"alloc.c\" unpacked with wrong size!
  330. fi
  331. # end of overwriting check
  332. fi
  333. if test -f bool.1 -a "${1}" != "-c" ; then 
  334.   echo shar: Will not over-write existing file \"bool.1\"
  335. else
  336. echo shar: Extracting \"bool.1\" \(5993 characters\)
  337. sed "s/^X//" >bool.1 <<'END_OF_bool.1'
  338. X.TH BOOL 1
  339. X.SH NAME
  340. Xbool \- boolean function array evaluator
  341. X.SH SYNOPSIS
  342. X.B bool
  343. X[
  344. X.B -noprint -DEBUG
  345. X] file.b [file2.b ...]
  346. X.SH DESCRIPTION
  347. XThis program reads boolean expressions from a file and produces output
  348. Xindicating the results of every possible input to the expression.  Its
  349. Xtheoretical input limit is unlimited, although the actual limit
  350. Xdepends on the amount of resources your machine will allow.  Multiple
  351. Xoutputs may be done by use of one equation per output.  An infinite
  352. Xnumber of outputs may be done (well, limited by your machine again) by
  353. Xplacing one boolean expression after another.  They will be evaluated
  354. Xin the order they are read.  Expressions may be broken between lines.
  355. XSpaces, tabs, returns, and newlines are ignored.
  356. X
  357. XExpression format accepted by
  358. X.I bool:
  359. X.nf
  360. X
  361. X  <equation> = <token> = <expr>;
  362. X  <expr>     = <expr> <opr> <expr> | (<expr>) | <not> <expr> |
  363. X               <expr> | <token> | <constant>
  364. X  <not>      = '!' | '~' | '/'
  365. X  <constant> = '1' | '0'
  366. X  <token>    = <alpha>[<alphanum>[<alphanum>...]]
  367. X  <alpha>    = 'a' .. 'z' | 'A' .. 'Z'
  368. X  <alphanum> = <alpha> | '0' .. '9'
  369. X  <opr>      = '|' | '+' | '*' | '&' | '^'
  370. X
  371. X  Operator Operation
  372. X  -------- ---------
  373. X     '+'      OR
  374. X     '|'      OR
  375. X     '*'      AND
  376. X     '&'      AND
  377. X     '/'      NOT
  378. X     '!'      NOT
  379. X     '~'      NOT
  380. X     '^'      XOR
  381. X.fi
  382. X.PP
  383. XThe following are two examples of valid equations:
  384. X.nf
  385. X
  386. X  o1= /i5 * (i4 * (i3 + /i3 * i2)) + i5;
  387. X  o2= !i5 & (i4 & (i3 | !i3 & i2)) | i5;
  388. X.fi
  389. X.PP
  390. XThese expressions are identical.
  391. X.SH PRECEDENCE OF OPERATORS
  392. X.PP
  393. XOperators have higher precedence as you move left in the equation.
  394. XPrecedence may be forced by using parentheses.  Expressions within
  395. Xparentheses are evaluated before those outside of parentheses.  For
  396. Xexample:
  397. X.nf
  398. X
  399. X  e1= x + y * z = (x + y) * z; [output of x OR y AND'ed with z]
  400. X  e2= x + (y * z) = y * z + x; [output of y AND z OR'ed with x]
  401. X.fi
  402. X.SH TOKENS
  403. X.PP
  404. XTokens are either inputs to an equation or equation identifiers.
  405. XTokens must start with an alphabetic character or an underscore.
  406. XSubsequent characters may include numerics.  Tokens may be any length
  407. Xbut only the first thirty-one characters or so are significant.
  408. X.PP
  409. XEquation identifiers are handled independently of inputs, so you may
  410. Xhave an equation identifier of the same name as an input.
  411. X.SH COMMENTS
  412. X.PP
  413. XComments may be
  414. Xincluded in the file; they are any characters surrounded by '[' and
  415. X']' or '{' and '}'.
  416. X.SH ERRORS
  417. X.PP
  418. XErrors in source files will be reported when they are discovered and
  419. X.B bool
  420. Xwill stop evaluating the file.  Usually a relevant error message
  421. Xwill be printed.
  422. X.SH FLAGS
  423. X.PP
  424. X-DEBUG causes
  425. X.B bool
  426. Xto print out additional information on how an
  427. Xequation was parsed.  This includes all parentheses (including
  428. Ximplicit parentheses) and errors if there were any.
  429. X.PP
  430. X-noprint suppresses the printing of the source file while it's being
  431. Xread.
  432. X.PP
  433. XAll files passed to
  434. X.B bool
  435. Xshould end in .b or they will be rejected by
  436. X.B bool.
  437. XThis was done to make it harder to pass garbage to
  438. X.B bool.
  439. X.SH PROBLEMS
  440. X.PP
  441. XAny problems you have with
  442. X.B bool
  443. Xshould be sent to madd@bucsf.bu.edu.
  444. X.B Bool
  445. Xhas built in problem checking -- if it discovers an
  446. Xinconsistency, it will print out a big irritating message to let you
  447. Xknow.  While debugging
  448. X.B bool
  449. Xthis message never once appeared, so I
  450. Xdon't expect that it will.  If it does, report it to me immediately
  451. Xand include the source file that caused the error.
  452. X.SH WHO NEEDS IT?
  453. X.PP
  454. XIn case you may be wondering what a boolean equation array evaluator
  455. Xis useful for, here's why I made it.  A friend of mine, who works for
  456. Xa relatively large company that produces a lot of electronic
  457. Xequipment, needed some way to determine every possible output from a
  458. Xseries of inputs to a PAL chip.  With 20 inputs and outputs, this is a
  459. Xpain to do by hand.  So he inquired if I could make an evaluator and
  460. Xhere it is.
  461. X.SH CHANGES
  462. X.PP
  463. XIf you make changes to the source code, please follow the instructions
  464. Xin the copyright.  I'd be interested in any improvements or bug fixes
  465. Xyou might make.
  466. X.SH AUTHOR
  467. X.nf
  468. Xjim frost
  469. Xmadd@bucsb.bu.edu
  470. X.fi
  471. X.SH NOTICE
  472. X.nf
  473. X(c) copyright 1987 jim frost
  474. Xall rights reserved
  475. X
  476. X.fi
  477. Xthis program is copyrighted material.  the author gives permission
  478. Xto duplicate and redistribute this program provided the following
  479. Xconditions are met:
  480. X.nf
  481. X  - this copyright notice is not removed.
  482. X  - all duplicate copies or distributions contain full source
  483. X    and documentation, including copyright notices.
  484. X  - duplicate copies or distributions outside a single site are
  485. X    original distributions without modifications.  (this is to keep
  486. X    bastardized versions from showing up all over thie place.)
  487. X.fi
  488. X.PP
  489. Xthis program source may be modified provided the following
  490. Xconditions are met:
  491. X.nf
  492. X  - modified source is not distributed to other sites.
  493. X  - modifications (including, but not limited to, bug fixes) are
  494. X    sent to the author if the modifications are to be distributed.
  495. X    no modified source is to be distributed unless done so by the
  496. X    author.
  497. X.fi
  498. X.PP
  499. Xno warranty, either express or implied, is given for this program.
  500. Xthe author makes no guarantees of fitness for any use of this
  501. Xprogram.  the author is not responsible for damages resulting from
  502. Xthe use of this program for any purpose.
  503. X.nf
  504. X
  505. X\'site\' refers to one or more computers under a single management.
  506. X\'author\' refers to the copyright holder, jim frost.
  507. X\'source\' refers to all files related to this program.
  508. X'documentation' refers to non-compilable files in the distribution.
  509. X.fi
  510. X.PP
  511. Xbasically this notice is to keep me out of trouble should anything
  512. Xgo wrong (i really *do* test these things though) and to make sure
  513. Xthat the distribution of code is centralized.  makes bug fixes and
  514. Xenhancements much easier.
  515. X.PP
  516. Xthank you for your attention to this copyright notice.  if everyone
  517. Xollows this, you may find this a useful tool that is pretty well
  518. Xsupported.
  519. X.SH FILES
  520. X.nf
  521. Xsample?.b - sample source files
  522. X.fi
  523. X.SH BUGS
  524. X.PP
  525. XNone so far.
  526. END_OF_bool.1
  527. if test 5993 -ne `wc -c <bool.1`; then
  528.     echo shar: \"bool.1\" unpacked with wrong size!
  529. fi
  530. # end of overwriting check
  531. fi
  532. if test -f bool.c -a "${1}" != "-c" ; then 
  533.   echo shar: Will not over-write existing file \"bool.c\"
  534. else
  535. echo shar: Extracting \"bool.c\" \(3685 characters\)
  536. sed "s/^X//" >bool.c <<'END_OF_bool.c'
  537. X/* (c) copyright 1987 jim frost
  538. X * all rights reserved
  539. X *
  540. X * this program is copyrighted material.  the author gives permission
  541. X * to duplicate and redistribute this program provided the following
  542. X * conditions are met:
  543. X *   - this copyright notice is not removed.
  544. X *   - all duplicate copies or distributions contain full source
  545. X *     and documentation, including copyright notices.
  546. X *   - duplicate copies or distributions outside a single site are
  547. X *     original distributions without modifications.  (this is to keep
  548. X *     bastardized versions from showing up all over thie place.)
  549. X *
  550. X * this program source may be modified provided the following
  551. X * conditions are met:
  552. X *   - modified source is not distributed to other sites.
  553. X *   - modifications (including, but not limited to, bug fixes) are
  554. X *     sent to the author if the modifications are to be distributed.
  555. X *     no modified source is to be distributed unless done so by the
  556. X *     author.
  557. X *
  558. X * no warranty, either express or implied, is given for this program.
  559. X * the author makes no guarantees of fitness for any use of this
  560. X * program.  the author is not responsible for damages resulting from
  561. X * the use of this program for any purpose.
  562. X *
  563. X * 'site' refers to one or more computers under a single management.
  564. X * 'author' refers to the copyright holder, jim frost.
  565. X * 'source' refers to all files related to this program.
  566. X * 'documentation' refers to non-compilable files in the distribution.
  567. X *
  568. X * basically this notice is to keep me out of trouble should anything
  569. X * go wrong (i really *do* test these things though) and to make sure
  570. X * that the distribution of code is centralized.  makes bug fixes and
  571. X * enhancements much easier.
  572. X *
  573. X * thank you for your attention to this copyright notice.  if everyone
  574. X * follows this, you may find this a useful tool that is pretty well
  575. X * supported.
  576. X *
  577. X * author information:
  578. X *   jim frost                    permanent usnail address:
  579. X *   madd@bucsb.bu.edu            75 washington street
  580. X *   ..!harvard!bu-cs!bucsb!madd  laconia, nh  03246.
  581. X */
  582. X/* bool.c:
  583. X *
  584. X * this program evaluates boolean equations and builds truth tables from
  585. X * the equations.
  586. X *
  587. X * history:
  588. X *   9.29.87    original version.
  589. X *   10.19.87   single expression (no operator) bugs fixed.  it couldn't
  590. X *              handle a single expression.  FALSE constant bug fixed.
  591. X *              returned TRUE by mistake.  typo.  changes made to output
  592. X *              to make it more readable.  bug in centering function of
  593. X *              the evaluation print routine fixed.  forgot to follow the
  594. X *              evaluated number by a space.
  595. X *   10.20.87   duplicate function declaration error fixed.  wouldn't
  596. X *              find duplicate function name if the duplicate was the
  597. X *              first in the function list.
  598. X */
  599. X
  600. X#include "bool.h"
  601. X
  602. Xint debug_mode,
  603. X    no_print;
  604. X
  605. Xmain(argc,argv)
  606. Xint argc;
  607. Xchar *argv[];
  608. X{ FILE *f;
  609. X  int a;
  610. X
  611. X  debug_mode= 0; /* no debugging mode unless -D */
  612. X  no_print= 0;   /* print unless -n */
  613. X
  614. X  if (argc == 1) {
  615. X    printf("Usage: %s [-DEBUG -noprint] filename [filename ...]\n",PROG);
  616. X    exit(1);
  617. X  }
  618. X
  619. X/*
  620. X * evaluate switches
  621. X */
  622. X
  623. X  for (a= 1; *argv[a] == '-'; a++) {
  624. X    if ((!strcmp(argv[a]+1,"DEBUG")) || (!strcmp(argv[a]+1,"D")))
  625. X      debug_mode= 1;
  626. X    else if ((!strcmp(argv[a]+1,"noprint")) || (!strcmp(argv[a]+1,"n")))
  627. X      no_print= 1;
  628. X  }
  629. X
  630. X/*
  631. X * evaluate for every file on command line
  632. X */
  633. X
  634. X  for (; a < argc; a++) {
  635. X    if (strcmp(argv[a]+strlen(argv[a])-2,".b"))
  636. X      printf("%s: not %s source file\n",argv[a],PROG);
  637. X    else if ((f= fopen(argv[a],"r")) != NULL) {
  638. X      eval_file(f);
  639. X      fclose(f);
  640. X    }
  641. X    else
  642. X      perror(argv[a]);
  643. X  }
  644. X}
  645. END_OF_bool.c
  646. if test 3685 -ne `wc -c <bool.c`; then
  647.     echo shar: \"bool.c\" unpacked with wrong size!
  648. fi
  649. # end of overwriting check
  650. fi
  651. if test -f bool.h -a "${1}" != "-c" ; then 
  652.   echo shar: Will not over-write existing file \"bool.h\"
  653. else
  654. echo shar: Extracting \"bool.h\" \(3766 characters\)
  655. sed "s/^X//" >bool.h <<'END_OF_bool.h'
  656. X/* (c) copyright 1987 jim frost
  657. X * all rights reserved
  658. X *
  659. X * this program is copyrighted material.  the author gives permission
  660. X * to duplicate and redistribute this program provided the following
  661. X * conditions are met:
  662. X *   - this copyright notice is not removed.
  663. X *   - all duplicate copies or distributions contain full source
  664. X *     and documentation, including copyright notices.
  665. X *   - duplicate copies or distributions outside a single site are
  666. X *     original distributions without modifications.  (this is to keep
  667. X *     bastardized versions from showing up all over thie place.)
  668. X *
  669. X * this program source may be modified provided the following
  670. X * conditions are met:
  671. X *   - modified source is not distributed to other sites.
  672. X *   - modifications (including, but not limited to, bug fixes) are
  673. X *     sent to the author if the modifications are to be distributed.
  674. X *     no modified source is to be distributed unless done so by the
  675. X *     author.
  676. X *
  677. X * no warranty, either express or implied, is given for this program.
  678. X * the author makes no guarantees of fitness for any use of this
  679. X * program.  the author is not responsible for damages resulting from
  680. X * the use of this program for any purpose.
  681. X *
  682. X * 'site' refers to one or more computers under a single management.
  683. X * 'author' refers to the copyright holder, jim frost.
  684. X * 'source' refers to all files related to this program.
  685. X * 'documentation' refers to non-compilable files in the distribution.
  686. X *
  687. X * basically this notice is to keep me out of trouble should anything
  688. X * go wrong (i really *do* test these things though) and to make sure
  689. X * that the distribution of code is centralized.  makes bug fixes and
  690. X * enhancements much easier.
  691. X *
  692. X * thank you for your attention to this copyright notice.  if everyone
  693. X * follows this, you may find this a useful tool that is pretty well
  694. X * supported.
  695. X *
  696. X * author information:
  697. X *   jim frost                    permanent usnail address:
  698. X *   madd@bucsb.bu.edu            75 washington street
  699. X *   ..!harvard!bu-cs!bucsb!madd  laconia, nh  03246.
  700. X */
  701. X/* bool.h:
  702. X *
  703. X * definition file for ttable.c
  704. X */
  705. X
  706. X#include <stdio.h>
  707. X
  708. X#define PROG     "bool"    /* our name */
  709. X#define MAXTOKEN 32        /* maximum length of a token */
  710. X
  711. Xtypedef struct bool_exp {
  712. X  int opcode,          /* operation code/constant/token */
  713. X      value;           /* token number/opcode char */
  714. X  char n1,             /* unary "not" indicators */
  715. X       n2;
  716. X  struct bool_exp *b1, /* boolean expressions */
  717. X                  *b2;
  718. X} BOOL_EXP;
  719. X
  720. X/*
  721. X * opcode field values
  722. X */
  723. X
  724. X#define UNDEF -1 /* undefined operation */
  725. X#define TRUE   0
  726. X#define FALSE  1
  727. X#define TOKEN  2
  728. X#define OR     3
  729. X#define AND    4
  730. X#define XOR    5
  731. X
  732. Xtypedef struct token_list {
  733. X  char token[MAXTOKEN];    /* character representation of the token */
  734. X  int  number,             /* numeric token representation */
  735. X       value;              /* current value of token */
  736. X  struct token_list *next; /* next token in list */
  737. X} TOKEN_LIST;
  738. X
  739. Xtypedef struct func_list {
  740. X  char func[MAXTOKEN];     /* name of this function */
  741. X  BOOL_EXP *bool_exp;      /* boolean expression */
  742. X  struct func_list *next;  /* next function in list */
  743. X} FUNC_LIST;
  744. X
  745. XBOOL_EXP   *build(),
  746. X           *newbnode();
  747. XFUNC_LIST  *new_func();
  748. XTOKEN_LIST *new_token();
  749. Xvoid       eval_file(),
  750. X           eval_func(),
  751. X           free_tree(),
  752. X           panic(),
  753. X           print_token(),
  754. X           print_tree();
  755. Xint        begtok(),
  756. X           eval_tree(),
  757. X           get_token(),
  758. X           intok(),
  759. X           token_val();
  760. Xchar       *malloc(),
  761. X           *strcpy();
  762. X
  763. X#define cget(F,C) if (!feof(F)) {\
  764. X                    C= fgetc(F);\
  765. X                    if ((!no_print) && (C != EOF))\
  766. X                      printf("%c",C);\
  767. X                  } else
  768. END_OF_bool.h
  769. if test 3766 -ne `wc -c <bool.h`; then
  770.     echo shar: \"bool.h\" unpacked with wrong size!
  771. fi
  772. # end of overwriting check
  773. fi
  774. if test -f build.c -a "${1}" != "-c" ; then 
  775.   echo shar: Will not over-write existing file \"build.c\"
  776. else
  777. echo shar: Extracting \"build.c\" \(6997 characters\)
  778. sed "s/^X//" >build.c <<'END_OF_build.c'
  779. X/* (c) copyright 1987 jim frost
  780. X * all rights reserved
  781. X *
  782. X * this program is copyrighted material.  the author gives permission
  783. X * to duplicate and redistribute this program provided the following
  784. X * conditions are met:
  785. X *   - this copyright notice is not removed.
  786. X *   - all duplicate copies or distributions contain full source
  787. X *     and documentation, including copyright notices.
  788. X *   - duplicate copies or distributions outside a single site are
  789. X *     original distributions without modifications.  (this is to keep
  790. X *     bastardized versions from showing up all over thie place.)
  791. X *
  792. X * this program source may be modified provided the following
  793. X * conditions are met:
  794. X *   - modified source is not distributed to other sites.
  795. X *   - modifications (including, but not limited to, bug fixes) are
  796. X *     sent to the author if the modifications are to be distributed.
  797. X *     no modified source is to be distributed unless done so by the
  798. X *     author.
  799. X *
  800. X * no warranty, either express or implied, is given for this program.
  801. X * the author makes no guarantees of fitness for any use of this
  802. X * program.  the author is not responsible for damages resulting from
  803. X * the use of this program for any purpose.
  804. X *
  805. X * 'site' refers to one or more computers under a single management.
  806. X * 'author' refers to the copyright holder, jim frost.
  807. X * 'source' refers to all files related to this program.
  808. X * 'documentation' refers to non-compilable files in the distribution.
  809. X *
  810. X * basically this notice is to keep me out of trouble should anything
  811. X * go wrong (i really *do* test these things though) and to make sure
  812. X * that the distribution of code is centralized.  makes bug fixes and
  813. X * enhancements much easier.
  814. X *
  815. X * thank you for your attention to this copyright notice.  if everyone
  816. X * follows this, you may find this a useful tool that is pretty well
  817. X * supported.
  818. X *
  819. X * author information:
  820. X *   jim frost                    permanent usnail address:
  821. X *   madd@bucsb.bu.edu            75 washington street
  822. X *   ..!harvard!bu-cs!bucsb!madd  laconia, nh  03246.
  823. X */
  824. X/* build.c:
  825. X *
  826. X * this reads in a file from a pointer and builds an evaluation tree.
  827. X */
  828. X
  829. X#include "bool.h"
  830. X
  831. Xextern int err,   /* syntax error detected */
  832. X           paren, /* parenthesis depth counter */
  833. X           no_print;
  834. X
  835. X/*
  836. X * this function checks a BOOL_EXP for syntax errors and sets "err"
  837. X * accordingly.
  838. X */
  839. X
  840. Xstatic void check_syn(t)
  841. XBOOL_EXP *t;
  842. X{
  843. X  switch (t->opcode) {
  844. X    case FALSE :
  845. X    case TRUE :
  846. X    case TOKEN :
  847. X      return;
  848. X    case AND :
  849. X    case OR :
  850. X    case XOR :
  851. X      if ((!t->b1) && (!t->b2)) {
  852. X        err= 1;
  853. X        printf("\nNull expression.\n");
  854. X      }
  855. X      else if (!t->b2) {
  856. X        err= 1;
  857. X        printf("\nMissing expression.\n");
  858. X      }
  859. X      return;
  860. X    case UNDEF :
  861. X      if ((t->b1) && (!(t->b2))) { /* single expression */
  862. X        check_syn(t->b1);
  863. X        return;
  864. X      }
  865. X      err= 1;
  866. X      printf("\nMissing operator.\n");
  867. X      t->value= '?';
  868. X      return;
  869. X    default :
  870. X      panic("check_syn");
  871. X  }
  872. X}
  873. X
  874. XBOOL_EXP *build(f)
  875. XFILE       *f;            /* file we are getting input from */
  876. X{ int      a;
  877. X  char     c,             /* current char we are reading */
  878. X           tok[MAXTOKEN]; /* current token we are reading */
  879. X  BOOL_EXP *t,*u;         /* current tree node and child tree node */
  880. X
  881. X/*
  882. X * if eof then make SURE we return null.  this will force a synerr
  883. X * during syntax checking above or an indicative error message from
  884. X * top level.
  885. X */
  886. X
  887. X  if (feof(f))
  888. X    return(NULL);
  889. X
  890. X/*
  891. X * get a new BOOL_EXP node to build with
  892. X */
  893. X
  894. X  t= newbnode();
  895. X
  896. X/*
  897. X * loop through the entire file.  a syntax error will automatically
  898. X * drop out of this loop and back up through the tree.  exiting is
  899. X * also done depending on conditions found in the source.
  900. X */
  901. X
  902. X  c= ' ';
  903. X  while (!feof(f))  {
  904. X
  905. X    if (c == ';') {
  906. X      check_syn(t);
  907. X      return(t);
  908. X    }
  909. X    if ((c == ' ') || (c == '\011') || (c == '\n') || (c == '\r')) {
  910. X      cget(f,c);
  911. X      continue;
  912. X    }
  913. X/*
  914. X * end subexression evaluation.  do syntax error checking at current
  915. X * node to be sure it's ok.  return status to caller.
  916. X */
  917. X
  918. X    if (c == ')') {
  919. X      if (!paren--) {
  920. X        err= 1;
  921. X        printf("\nMismatched parenthesis.\n");
  922. X      }
  923. X      else
  924. X        check_syn(t);
  925. X      return(t);
  926. X    }
  927. X
  928. X/*
  929. X * if structure is full then we have to make it a substruct.  note
  930. X * that changing this can alter precedence determination from left
  931. X * to right or right to left.
  932. X */
  933. X
  934. X    if (t->b2) {
  935. X      u= t;
  936. X      t= newbnode();
  937. X      t->b1= u;
  938. X    }
  939. X
  940. X    if (begtok(c)) { /* found a token so evaluate it */
  941. X
  942. X/*
  943. X * loop for token name
  944. X */
  945. X
  946. X      a= 0;
  947. X      do {
  948. X        if (a < MAXTOKEN) /* truncate overlong tokens */
  949. X          tok[a]= c;
  950. X        a++;
  951. X        if (feof(f))
  952. X          break;
  953. X        cget(f,c);
  954. X      } while (intok(c));
  955. X      tok[a]= '\0';
  956. X
  957. X/*
  958. X * build node for token
  959. X */
  960. X
  961. X      u= newbnode();
  962. X      u->opcode= TOKEN;
  963. X      u->value= get_token(tok);
  964. X      if (t->b1)
  965. X        t->b2= u;
  966. X      else
  967. X        t->b1= u;
  968. X    }
  969. X    else {
  970. X      switch (c) {
  971. X
  972. X/*
  973. X * comments.  this doesn't have a possible backslash preceeding it because
  974. X * I really don't care if you want to put special chars in your comments.
  975. X * two different comment types are used to allow you to comment out whole
  976. X * sections of code, which IS useful.
  977. X */
  978. X
  979. X        case '[' :
  980. X          while (c != ']') {
  981. X            if (feof(f)) {
  982. X              err= 1;
  983. X              return(t);
  984. X            }
  985. X            cget(f,c);
  986. X          }
  987. X          break;
  988. X        case '{' :
  989. X          while (c != '}') {
  990. X            if (feof(f)) {
  991. X              err= 1;
  992. X              return(t);
  993. X            }
  994. X            cget(f,c);
  995. X          }
  996. X          break;
  997. X
  998. X/*
  999. X * true or false
  1000. X */
  1001. X
  1002. X        case '1' :
  1003. X        case '0' :
  1004. X          u= newbnode();
  1005. X          if (t->b1)
  1006. X            t->b2= u;
  1007. X          else
  1008. X            t->b1= u;
  1009. X          if (c == '0')
  1010. X            u->opcode= FALSE;
  1011. X          else
  1012. X            u->opcode= TRUE;
  1013. X          break;
  1014. X          
  1015. X/*
  1016. X * subexpressions begin with a paren
  1017. X */
  1018. X
  1019. X        case '(' :
  1020. X          paren++;
  1021. X          if (t->b1)
  1022. X            t->b2= build(f);
  1023. X          else
  1024. X            t->b1= build(f);
  1025. X          if (err)
  1026. X            return(t);
  1027. X          break;
  1028. X
  1029. X/*
  1030. X * handle each unary operation
  1031. X */
  1032. X
  1033. X        case '!' : /* not */
  1034. X        case '~' :
  1035. X        case '/' :
  1036. X          if (t->b1)
  1037. X            t->n2= c;
  1038. X          else
  1039. X            t->n1= c;
  1040. X          break;
  1041. X
  1042. X/*
  1043. X * handle each of the binary operators
  1044. X */
  1045. X
  1046. X        case '&' : /* and */
  1047. X        case '*' :
  1048. X          t->opcode= AND;
  1049. X          t->value= c;
  1050. X          break;
  1051. X        case '|' : /* or */
  1052. X        case '+' :
  1053. X          t->opcode= OR;
  1054. X          t->value= c;
  1055. X          break;
  1056. X        case '^' : /* xor */
  1057. X          t->opcode= XOR;
  1058. X          t->value= c;
  1059. X          break;
  1060. X        default :
  1061. X          err= 1;
  1062. X          printf("\nIllegal character '%c'\n",c);
  1063. X          return(t);
  1064. X      }
  1065. X      cget(f,c);
  1066. X    }
  1067. X  }
  1068. X  check_syn(t); /* check for syntax errors before leaving */
  1069. X  return(t);
  1070. X}
  1071. END_OF_build.c
  1072. if test 6997 -ne `wc -c <build.c`; then
  1073.     echo shar: \"build.c\" unpacked with wrong size!
  1074. fi
  1075. # end of overwriting check
  1076. fi
  1077. if test -f eval_file.c -a "${1}" != "-c" ; then 
  1078.   echo shar: Will not over-write existing file \"eval_file.c\"
  1079. else
  1080. echo shar: Extracting \"eval_file.c\" \(5560 characters\)
  1081. sed "s/^X//" >eval_file.c <<'END_OF_eval_file.c'
  1082. X/* (c) copyright 1987 jim frost
  1083. X * all rights reserved
  1084. X *
  1085. X * this program is copyrighted material.  the author gives permission
  1086. X * to duplicate and redistribute this program provided the following
  1087. X * conditions are met:
  1088. X *   - this copyright notice is not removed.
  1089. X *   - all duplicate copies or distributions contain full source
  1090. X *     and documentation, including copyright notices.
  1091. X *   - duplicate copies or distributions outside a single site are
  1092. X *     original distributions without modifications.  (this is to keep
  1093. X *     bastardized versions from showing up all over thie place.)
  1094. X *
  1095. X * this program source may be modified provided the following
  1096. X * conditions are met:
  1097. X *   - modified source is not distributed to other sites.
  1098. X *   - modifications (including, but not limited to, bug fixes) are
  1099. X *     sent to the author if the modifications are to be distributed.
  1100. X *     no modified source is to be distributed unless done so by the
  1101. X *     author.
  1102. X *
  1103. X * no warranty, either express or implied, is given for this program.
  1104. X * the author makes no guarantees of fitness for any use of this
  1105. X * program.  the author is not responsible for damages resulting from
  1106. X * the use of this program for any purpose.
  1107. X *
  1108. X * 'site' refers to one or more computers under a single management.
  1109. X * 'author' refers to the copyright holder, jim frost.
  1110. X * 'source' refers to all files related to this program.
  1111. X * 'documentation' refers to non-compilable files in the distribution.
  1112. X *
  1113. X * basically this notice is to keep me out of trouble should anything
  1114. X * go wrong (i really *do* test these things though) and to make sure
  1115. X * that the distribution of code is centralized.  makes bug fixes and
  1116. X * enhancements much easier.
  1117. X *
  1118. X * thank you for your attention to this copyright notice.  if everyone
  1119. X * follows this, you may find this a useful tool that is pretty well
  1120. X * supported.
  1121. X *
  1122. X * author information:
  1123. X *   jim frost                    permanent usnail address:
  1124. X *   madd@bucsb.bu.edu            75 washington street
  1125. X *   ..!harvard!bu-cs!bucsb!madd  laconia, nh  03246.
  1126. X */
  1127. X/* eval_file.c:
  1128. X *
  1129. X * this function calls each of the necessary routines to build and
  1130. X * evaluate a tree.
  1131. X */
  1132. X
  1133. X#include "bool.h"
  1134. X
  1135. Xint err,
  1136. X    paren,
  1137. X    max_token,
  1138. X    print_err;
  1139. X
  1140. Xextern int debug_mode,
  1141. X           no_print;
  1142. X
  1143. Xstatic char *line= "----------------------------------------------------";
  1144. X
  1145. XTOKEN_LIST *t_list;
  1146. XFUNC_LIST  *f_list;
  1147. X
  1148. X/*
  1149. X * this function processes each boolean equation
  1150. X */
  1151. X
  1152. Xvoid eval_file(f)
  1153. XFILE *f;
  1154. X{ FUNC_LIST  *fn;
  1155. X  TOKEN_LIST *t;
  1156. X  char       func[MAXTOKEN],
  1157. X             c;
  1158. X  int        a;
  1159. X
  1160. X  paren= 0;
  1161. X  max_token= 0;
  1162. X  t_list= NULL;
  1163. X  f_list= NULL;
  1164. X
  1165. X/*
  1166. X * loop through file to build all functions
  1167. X */
  1168. X
  1169. X  func[0]= '\0';
  1170. X  c= ' ';
  1171. X  while (!feof(f)) {
  1172. X    if (blank(c)) {
  1173. X      cget(f,c);
  1174. X    }
  1175. X    else if (begtok(c)) {
  1176. X      a= 0;
  1177. X      do {
  1178. X        func[a++]= c;
  1179. X        cget(f,c);
  1180. X      } while(intok(c));
  1181. X      func[a]= '\0';
  1182. X    }
  1183. X
  1184. X/*
  1185. X * when we hit an '=', start building a function
  1186. X */
  1187. X
  1188. X    else if ((c == '=') && strlen(func)) {
  1189. X      fn= new_func(func);
  1190. X      fn->bool_exp= build(f);
  1191. X
  1192. X/*
  1193. X * if we got a syntax error, explain that we're printing the function
  1194. X * as evaluated up to the error.  otherwise, print out the header and
  1195. X * do the tree evaluation.
  1196. X */
  1197. X
  1198. X      if (err) {
  1199. X        if (debug_mode) {
  1200. X          printf("Parsed function up to error:\n");
  1201. X          print_err= 0;
  1202. X          printf("%s= ",func);
  1203. X          print_tree(fn->bool_exp);
  1204. X          printf("\n");
  1205. X        }
  1206. X        exit(1);
  1207. X      }
  1208. X      if (debug_mode) {
  1209. X        printf("\n\nFunction parsed as:\n");
  1210. X        printf("%s= ",func);
  1211. X        print_tree(fn->bool_exp);
  1212. X        printf(";\n");
  1213. X      }
  1214. X      func[0]= '\0';    /* reset for next run */
  1215. X      cget(f,c);
  1216. X    }
  1217. X    else switch (c) {
  1218. X      case '[' :
  1219. X        do
  1220. X          cget(f,c);
  1221. X        while ((!feof(f)) && (c != ']'));
  1222. X        cget(f,c);
  1223. X        break;
  1224. X      case '{' :
  1225. X        do
  1226. X          cget(f,c);
  1227. X        while ((!feof(f)) && (c != '}'));
  1228. X        cget(f,c);
  1229. X        break;
  1230. X      case '=' :
  1231. X        printf("\nMissing equation declaration.\n");
  1232. X        exit(1);
  1233. X      case EOF :
  1234. X        printf("\nUnexpected end of file.\n");
  1235. X        exit(1);
  1236. X      default :
  1237. X        printf("\nIllegal character (possibly missing equation definition).\n");
  1238. X        exit(1);
  1239. X    }
  1240. X  }
  1241. X  printf("\n");
  1242. X  t= t_list;
  1243. X  while (t) {
  1244. X    printf("%s ",t->token);
  1245. X    t= t->next;
  1246. X  }
  1247. X  printf("| ");
  1248. X  fn= f_list;
  1249. X  while (fn) {
  1250. X    printf("%s ",fn->func);
  1251. X    fn= fn->next;
  1252. X  }
  1253. X  printf("\n");
  1254. X  t= t_list;
  1255. X  while (t) {
  1256. X    printf("%*.*s",strlen(t->token)+1,strlen(t->token)+1,line);
  1257. X    t= t->next;
  1258. X  }
  1259. X  printf("+-");
  1260. X  fn= f_list;
  1261. X  while (fn) {
  1262. X    printf("%*.*s",strlen(fn->func)+1,strlen(fn->func)+1,line);
  1263. X    fn= fn->next;
  1264. X  }
  1265. X  printf("\n");
  1266. X  eval_func(t_list); /* dump output */
  1267. X
  1268. X/*
  1269. X * because bool can be run on a lot of files each execution, we have
  1270. X * to free up memory allocated for each file.  while we're at it, we
  1271. X * print out lists of stuff used in the file if we're in debug mode.
  1272. X * might be useful to somebody.
  1273. X */
  1274. X
  1275. X  if (debug_mode) {
  1276. X    printf("\n\nFunctions Defined\n");
  1277. X    printf("-----------------\n");
  1278. X  }
  1279. X  while (f_list) {
  1280. X    if (debug_mode)
  1281. X      printf("%s\n",f_list->func);
  1282. X    fn= f_list;
  1283. X    f_list= f_list->next;
  1284. X    free_tree(fn->bool_exp);
  1285. X    free((char *)fn);
  1286. X  }
  1287. X  if (debug_mode) {
  1288. X    printf("\n\nTokens Defined\n");
  1289. X    printf("--------------\n");
  1290. X  }
  1291. X  while (t_list) {
  1292. X    if (debug_mode)
  1293. X      printf("%s\n",t_list->token);
  1294. X    t= t_list;
  1295. X    t_list= t_list->next;
  1296. X    free((char *)t);
  1297. X  }
  1298. X}
  1299. END_OF_eval_file.c
  1300. if test 5560 -ne `wc -c <eval_file.c`; then
  1301.     echo shar: \"eval_file.c\" unpacked with wrong size!
  1302. fi
  1303. # end of overwriting check
  1304. fi
  1305. if test -f eval_func.c -a "${1}" != "-c" ; then 
  1306.   echo shar: Will not over-write existing file \"eval_func.c\"
  1307. else
  1308. echo shar: Extracting \"eval_func.c\" \(3865 characters\)
  1309. sed "s/^X//" >eval_func.c <<'END_OF_eval_func.c'
  1310. X/* (c) copyright 1987 jim frost
  1311. X * all rights reserved
  1312. X *
  1313. X * this program is copyrighted material.  the author gives permission
  1314. X * to duplicate and redistribute this program provided the following
  1315. X * conditions are met:
  1316. X *   - this copyright notice is not removed.
  1317. X *   - all duplicate copies or distributions contain full source
  1318. X *     and documentation, including copyright notices.
  1319. X *   - duplicate copies or distributions outside a single site are
  1320. X *     original distributions without modifications.  (this is to keep
  1321. X *     bastardized versions from showing up all over thie place.)
  1322. X *
  1323. X * this program source may be modified provided the following
  1324. X * conditions are met:
  1325. X *   - modified source is not distributed to other sites.
  1326. X *   - modifications (including, but not limited to, bug fixes) are
  1327. X *     sent to the author if the modifications are to be distributed.
  1328. X *     no modified source is to be distributed unless done so by the
  1329. X *     author.
  1330. X *
  1331. X * no warranty, either express or implied, is given for this program.
  1332. X * the author makes no guarantees of fitness for any use of this
  1333. X * program.  the author is not responsible for damages resulting from
  1334. X * the use of this program for any purpose.
  1335. X *
  1336. X * 'site' refers to one or more computers under a single management.
  1337. X * 'author' refers to the copyright holder, jim frost.
  1338. X * 'source' refers to all files related to this program.
  1339. X * 'documentation' refers to non-compilable files in the distribution.
  1340. X *
  1341. X * basically this notice is to keep me out of trouble should anything
  1342. X * go wrong (i really *do* test these things though) and to make sure
  1343. X * that the distribution of code is centralized.  makes bug fixes and
  1344. X * enhancements much easier.
  1345. X *
  1346. X * thank you for your attention to this copyright notice.  if everyone
  1347. X * follows this, you may find this a useful tool that is pretty well
  1348. X * supported.
  1349. X *
  1350. X * author information:
  1351. X *   jim frost                    permanent usnail address:
  1352. X *   madd@bucsb.bu.edu            75 washington street
  1353. X *   ..!harvard!bu-cs!bucsb!madd  laconia, nh  03246.
  1354. X */
  1355. X/* eval_func.c:
  1356. X *
  1357. X * this contains boolean expression evaluation routines.
  1358. X */
  1359. X
  1360. X#include "bool.h"
  1361. X
  1362. Xextern FUNC_LIST  *f_list;
  1363. Xextern TOKEN_LIST *t_list;
  1364. Xextern int        line;
  1365. X/*
  1366. X * this function evaluates a boolean expression tree
  1367. X */
  1368. X
  1369. Xint eval_exp(b)
  1370. XBOOL_EXP *b;
  1371. X{ int e1, e2;
  1372. X
  1373. X  if (b->opcode > TOKEN) {
  1374. X    e1= eval_exp(b->b1);
  1375. X    e2= eval_exp(b->b2);
  1376. X    if (b->n1)
  1377. X      e1= !e1;
  1378. X    if (b->n2)
  1379. X      e2= !e2;
  1380. X  }
  1381. X  switch(b->opcode) {
  1382. X    case UNDEF : /* single expressions have undefined opcodes */
  1383. X      if (b->n1)
  1384. X        return(!eval_exp(b->b1));
  1385. X      else
  1386. X        return(eval_exp(b->b1));
  1387. X    case TRUE :
  1388. X      return(1);
  1389. X    case FALSE :
  1390. X      return(0);
  1391. X    case TOKEN :
  1392. X      return(token_val(b->value));
  1393. X    case OR :
  1394. X      return(e1 || e2);
  1395. X    case AND :
  1396. X      return(e1 && e2);
  1397. X    case XOR :
  1398. X      return((e1 || e2) && (!(e1 && e2)));
  1399. X  }
  1400. X}
  1401. X
  1402. X/*
  1403. X * this routine evaluates a function for all possible values of all tokens
  1404. X */
  1405. X
  1406. Xvoid eval_func(t)
  1407. XTOKEN_LIST   *t;
  1408. X{ FUNC_LIST  *f;
  1409. X  TOKEN_LIST *u;
  1410. X  int        l1,l2;
  1411. X
  1412. X  if (t == NULL) { /* end of list so do evaluations */
  1413. X
  1414. X/*
  1415. X * print out values of each token
  1416. X */
  1417. X
  1418. X    u= t_list;
  1419. X    while (u) {
  1420. X      l1= l2= strlen(u->token)/2;
  1421. X      if (!(strlen(u->token) % 2))
  1422. X        l1--;
  1423. X      printf("%*.*s%d%*.*s ",l1,l1,"",u->value,l2,l2,"");
  1424. X      u= u->next;
  1425. X    }
  1426. X    printf("| ");
  1427. X
  1428. X/*
  1429. X * print out values of each tree
  1430. X */
  1431. X
  1432. X    f= f_list;
  1433. X    while(f) {
  1434. X      l1= l2= strlen(f->func)/2;
  1435. X      if (!(strlen(f->func) % 2))
  1436. X        l1--;
  1437. X      printf("%*.*s%d%*.*s ",l1,l1,"",eval_exp(f->bool_exp),l2,l2,"");
  1438. X      f= f->next;
  1439. X    }
  1440. X    printf("\n");
  1441. X    return;
  1442. X  }
  1443. X
  1444. X/*
  1445. X * this section handles value setting to evaluate for all possible
  1446. X * token values.
  1447. X */
  1448. X
  1449. X  t->value= 0;
  1450. X  eval_func(t->next);
  1451. X  t->value= 1;
  1452. X  eval_func(t->next);
  1453. X}
  1454. END_OF_eval_func.c
  1455. if test 3865 -ne `wc -c <eval_func.c`; then
  1456.     echo shar: \"eval_func.c\" unpacked with wrong size!
  1457. fi
  1458. # end of overwriting check
  1459. fi
  1460. if test -f misc.c -a "${1}" != "-c" ; then 
  1461.   echo shar: Will not over-write existing file \"misc.c\"
  1462. else
  1463. echo shar: Extracting \"misc.c\" \(3754 characters\)
  1464. sed "s/^X//" >misc.c <<'END_OF_misc.c'
  1465. X/* (c) copyright 1987 jim frost
  1466. X * all rights reserved
  1467. X *
  1468. X * this program is copyrighted material.  the author gives permission
  1469. X * to duplicate and redistribute this program provided the following
  1470. X * conditions are met:
  1471. X *   - this copyright notice is not removed.
  1472. X *   - all duplicate copies or distributions contain full source
  1473. X *     and documentation, including copyright notices.
  1474. X *   - duplicate copies or distributions outside a single site are
  1475. X *     original distributions without modifications.  (this is to keep
  1476. X *     bastardized versions from showing up all over thie place.)
  1477. X *
  1478. X * this program source may be modified provided the following
  1479. X * conditions are met:
  1480. X *   - modified source is not distributed to other sites.
  1481. X *   - modifications (including, but not limited to, bug fixes) are
  1482. X *     sent to the author if the modifications are to be distributed.
  1483. X *     no modified source is to be distributed unless done so by the
  1484. X *     author.
  1485. X *
  1486. X * no warranty, either express or implied, is given for this program.
  1487. X * the author makes no guarantees of fitness for any use of this
  1488. X * program.  the author is not responsible for damages resulting from
  1489. X * the use of this program for any purpose.
  1490. X *
  1491. X * 'site' refers to one or more computers under a single management.
  1492. X * 'author' refers to the copyright holder, jim frost.
  1493. X * 'source' refers to all files related to this program.
  1494. X * 'documentation' refers to non-compilable files in the distribution.
  1495. X *
  1496. X * basically this notice is to keep me out of trouble should anything
  1497. X * go wrong (i really *do* test these things though) and to make sure
  1498. X * that the distribution of code is centralized.  makes bug fixes and
  1499. X * enhancements much easier.
  1500. X *
  1501. X * thank you for your attention to this copyright notice.  if everyone
  1502. X * follows this, you may find this a useful tool that is pretty well
  1503. X * supported.
  1504. X *
  1505. X * author information:
  1506. X *   jim frost                    permanent usnail address:
  1507. X *   madd@bucsb.bu.edu            75 washington street
  1508. X *   ..!harvard!bu-cs!bucsb!madd  laconia, nh  03246.
  1509. X */
  1510. X/* misc.c:
  1511. X *
  1512. X * any miscellaneous functions that i didn't think belonged anywhere
  1513. X * else.
  1514. X */
  1515. X
  1516. X#include "bool.h"
  1517. X
  1518. Xextern int print_err;
  1519. X
  1520. Xint blank(c)
  1521. Xchar c;
  1522. X{
  1523. X  return((c == ' ') || (c == '\011') || (c == '\n') || (c == '\r'));
  1524. X}
  1525. X
  1526. X/*
  1527. X * this function prints out the tree.  if the tree is incomplete, it
  1528. X * just stops.
  1529. X */
  1530. X
  1531. Xvoid print_tree(b)
  1532. XBOOL_EXP *b;
  1533. X{ if (b == NULL) {
  1534. X    if (!print_err) {
  1535. X      printf("?");
  1536. X      print_err= 1;
  1537. X    }
  1538. X    return;
  1539. X  }
  1540. X  switch (b->opcode) {
  1541. X    case FALSE :
  1542. X      printf("0");
  1543. X      break;
  1544. X    case TRUE :
  1545. X      printf("1");
  1546. X      break;
  1547. X    case TOKEN :
  1548. X      print_token(b->value);
  1549. X      break;
  1550. X    case UNDEF :    /* all of these have a char stored in b-value */
  1551. X      if ((b->b1) && (!b->b2)) {
  1552. X        if (b->n1)
  1553. X          printf("!");
  1554. X        print_tree(b->b1);
  1555. X      }
  1556. X      else
  1557. X        print_err= 1;
  1558. X      break;
  1559. X    case OR :
  1560. X    case AND :
  1561. X    case XOR :
  1562. X      printf("(");
  1563. X      if (b->n1)                     /* left not */
  1564. X        printf("%c",b->n1);
  1565. X      print_tree(b->b1);             /* left tree */
  1566. X      printf(" %c ",(char)b->value); /* operator */
  1567. X      if (b->n2)                     /* right not */
  1568. X        printf("%c",b->n2);
  1569. X      print_tree(b->b2);             /* right tree */
  1570. X      if (b->b2)                     /* only print if not syntax err */
  1571. X        printf(")");
  1572. X      break;
  1573. X    default :
  1574. X      panic("print_tree");
  1575. X  }
  1576. X}
  1577. X
  1578. X
  1579. X/*
  1580. X * we got a real serious error
  1581. X */
  1582. X
  1583. Xvoid panic(s)
  1584. Xchar *s;
  1585. X{
  1586. X  printf("\n\nPANIC!!  Internal program error in %s\n",s);
  1587. X  printf("This error should not have happened.  Please contact you distributer\n");
  1588. X  printf("and keep a sample of the data that caused this crash.\n");
  1589. X  exit(1);
  1590. X}
  1591. X
  1592. END_OF_misc.c
  1593. if test 3754 -ne `wc -c <misc.c`; then
  1594.     echo shar: \"misc.c\" unpacked with wrong size!
  1595. fi
  1596. # end of overwriting check
  1597. fi
  1598. if test -f sample1.b -a "${1}" != "-c" ; then 
  1599.   echo shar: Will not over-write existing file \"sample1.b\"
  1600. else
  1601. echo shar: Extracting \"sample1.b\" \(358 characters\)
  1602. sed "s/^X//" >sample1.b <<'END_OF_sample1.b'
  1603. X[* this is a bool sample source file.  it's really simple but it shows
  1604. X * the format of a bool source file and shows how the different types
  1605. X * of comment delimiters can be used. ]
  1606. X
  1607. Xtest= a + b * !c; [ first equation ]
  1608. X
  1609. X{ we decided we didn't like this equation:
  1610. Xmissing= a ^ b;   [ equation that we are taking out ]
  1611. X}
  1612. X
  1613. Xtest2 = a | c;    [ second equation ]
  1614. END_OF_sample1.b
  1615. if test 358 -ne `wc -c <sample1.b`; then
  1616.     echo shar: \"sample1.b\" unpacked with wrong size!
  1617. fi
  1618. # end of overwriting check
  1619. fi
  1620. if test -f sample2.b -a "${1}" != "-c" ; then 
  1621.   echo shar: Will not over-write existing file \"sample2.b\"
  1622. else
  1623. echo shar: Extracting \"sample2.b\" \(147 characters\)
  1624. sed "s/^X//" >sample2.b <<'END_OF_sample2.b'
  1625. X[* this is a sample bool source file.  it demonstrates precedence forcing. ]
  1626. X
  1627. Xfoo   = this & !is | (a + little) ^ test;
  1628. Xfubar = little | test ^ 1;
  1629. END_OF_sample2.b
  1630. if test 147 -ne `wc -c <sample2.b`; then
  1631.     echo shar: \"sample2.b\" unpacked with wrong size!
  1632. fi
  1633. # end of overwriting check
  1634. fi
  1635. if test -f sample3.b -a "${1}" != "-c" ; then 
  1636.   echo shar: Will not over-write existing file \"sample3.b\"
  1637. else
  1638. echo shar: Extracting \"sample3.b\" \(305 characters\)
  1639. sed "s/^X//" >sample3.b <<'END_OF_sample3.b'
  1640. X[* this is a sample bool source file.  it contains an error -- the expression
  1641. X * following the operator '&' is missing.  it demonstrates the kind of error
  1642. X * trapping that bool does.  you might want to try using the -DEBUG flag to
  1643. X * see the equation that bool thought this was. ]
  1644. X
  1645. Xexplode= this + !is &;
  1646. END_OF_sample3.b
  1647. if test 305 -ne `wc -c <sample3.b`; then
  1648.     echo shar: \"sample3.b\" unpacked with wrong size!
  1649. fi
  1650. # end of overwriting check
  1651. fi
  1652. if test -f sample4.b -a "${1}" != "-c" ; then 
  1653.   echo shar: Will not over-write existing file \"sample4.b\"
  1654. else
  1655. echo shar: Extracting \"sample4.b\" \(509 characters\)
  1656. sed "s/^X//" >sample4.b <<'END_OF_sample4.b'
  1657. X[* this is a bool sample source file.  it was an actual assignment that
  1658. X * a couple of friends of mine had and it pointed out a series of bugs
  1659. X * that i had missed.]
  1660. X
  1661. Xf0= !y | (w & !x & !z);        [ y' + wx'z' ]
  1662. Xf1= x & !y;                    [ xy' ]
  1663. Xf2= !x & !z;                   [ x'z' ]
  1664. Xf3= (x & z) | (w & !x & !y);   [ xz + wx'y' ]
  1665. Xf4= !w;                        [ w' ]
  1666. Xf5= w ^ x ^ y ^ z;             [ w xor x xor y xor z ]
  1667. Xf6= !x & y & !z;               [ x'yz' ]
  1668. Xf7= 1;                         [ 1 ]
  1669. END_OF_sample4.b
  1670. if test 509 -ne `wc -c <sample4.b`; then
  1671.     echo shar: \"sample4.b\" unpacked with wrong size!
  1672. fi
  1673. # end of overwriting check
  1674. fi
  1675. if test -f sample5.b -a "${1}" != "-c" ; then 
  1676.   echo shar: Will not over-write existing file \"sample5.b\"
  1677. else
  1678. echo shar: Extracting \"sample5.b\" \(431 characters\)
  1679. sed "s/^X//" >sample5.b <<'END_OF_sample5.b'
  1680. X[* this is a really big example in an attempt to see what will happen.
  1681. X]
  1682. X
  1683. Xf1= a ^ b ^ c ^ d ^ e ^ f ^ g ^ h ^ i ^ j ^ k ^ l ^ m ^ n ^ o ^
  1684. X   p ^ q ^ r ^ s ^ t ^ u ^ v ^ w ^ x ^ y ^ z;
  1685. Xf2= !a ^ !b ^ !c ^ !d ^ !e ^ !f ^ !g ^ !h ^ !i ^ !j ^ !k ^ !l ^ !m ^ !n ^ !o ^
  1686. X   !p ^ !q ^ !r ^ !s ^ !t ^ !u ^ !v ^ !w ^ !x ^ !y ^ !z;
  1687. Xf3= a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + 
  1688. X   p + q + r + s + t + u + v + w + x + y + z;
  1689. END_OF_sample5.b
  1690. if test 431 -ne `wc -c <sample5.b`; then
  1691.     echo shar: \"sample5.b\" unpacked with wrong size!
  1692. fi
  1693. # end of overwriting check
  1694. fi
  1695. if test -f token.c -a "${1}" != "-c" ; then 
  1696.   echo shar: Will not over-write existing file \"token.c\"
  1697. else
  1698. echo shar: Extracting \"token.c\" \(3916 characters\)
  1699. sed "s/^X//" >token.c <<'END_OF_token.c'
  1700. X/* (c) copyright 1987 jim frost
  1701. X * all rights reserved
  1702. X *
  1703. X * this program is copyrighted material.  the author gives permission
  1704. X * to duplicate and redistribute this program provided the following
  1705. X * conditions are met:
  1706. X *   - this copyright notice is not removed.
  1707. X *   - all duplicate copies or distributions contain full source
  1708. X *     and documentation, including copyright notices.
  1709. X *   - duplicate copies or distributions outside a single site are
  1710. X *     original distributions without modifications.  (this is to keep
  1711. X *     bastardized versions from showing up all over thie place.)
  1712. X *
  1713. X * this program source may be modified provided the following
  1714. X * conditions are met:
  1715. X *   - modified source is not distributed to other sites.
  1716. X *   - modifications (including, but not limited to, bug fixes) are
  1717. X *     sent to the author if the modifications are to be distributed.
  1718. X *     no modified source is to be distributed unless done so by the
  1719. X *     author.
  1720. X *
  1721. X * no warranty, either express or implied, is given for this program.
  1722. X * the author makes no guarantees of fitness for any use of this
  1723. X * program.  the author is not responsible for damages resulting from
  1724. X * the use of this program for any purpose.
  1725. X *
  1726. X * 'site' refers to one or more computers under a single management.
  1727. X * 'author' refers to the copyright holder, jim frost.
  1728. X * 'source' refers to all files related to this program.
  1729. X * 'documentation' refers to non-compilable files in the distribution.
  1730. X *
  1731. X * basically this notice is to keep me out of trouble should anything
  1732. X * go wrong (i really *do* test these things though) and to make sure
  1733. X * that the distribution of code is centralized.  makes bug fixes and
  1734. X * enhancements much easier.
  1735. X *
  1736. X * thank you for your attention to this copyright notice.  if everyone
  1737. X * follows this, you may find this a useful tool that is pretty well
  1738. X * supported.
  1739. X *
  1740. X * author information:
  1741. X *   jim frost                    permanent usnail address:
  1742. X *   madd@bucsb.bu.edu            75 washington street
  1743. X *   ..!harvard!bu-cs!bucsb!madd  laconia, nh  03246.
  1744. X */
  1745. X/* token.c:
  1746. X *
  1747. X * this file contains token creation, insertion, print, and value funcs.
  1748. X */
  1749. X
  1750. X#include "bool.h"
  1751. X
  1752. Xextern TOKEN_LIST *t_list;
  1753. X
  1754. X/*
  1755. X * these functions return a boolean value dependent on whether or
  1756. X * not a char is a legal char inside a token.
  1757. X */
  1758. X
  1759. Xint begtok(c)
  1760. Xchar c;
  1761. X{
  1762. X  return(((c >='@') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) ||
  1763. X         (c == '_'));
  1764. X}
  1765. X
  1766. Xint intok(c)
  1767. Xchar c;
  1768. X{
  1769. X  return(begtok(c) || ((c >='0') && (c <='9')));
  1770. X}
  1771. X
  1772. X/*
  1773. X * this looks through the token list and returns token values.  if no
  1774. X * token exists, it is added.
  1775. X */
  1776. X
  1777. Xint get_token(s)
  1778. Xchar *s;
  1779. X{ TOKEN_LIST *t, *u;
  1780. X
  1781. X  if (!t_list)
  1782. X    return((t_list= new_token(s))->number); /* null list so make one */
  1783. X
  1784. X/*
  1785. X * try to find token in list
  1786. X */
  1787. X
  1788. X  t= t_list;
  1789. X  for (;;) {                 /* we should always return() out of this */
  1790. X    if (!strcmp(t->token,s)) /* found it so return its number */
  1791. X      return(t->number);
  1792. X    else if (t->next)        /* if not end of list, keep looking */
  1793. X      t= t->next;
  1794. X    else {                   /* end of list so make new token */
  1795. X      if (strcmp(s,t_list->token) < 0) {
  1796. X        t= new_token(s);
  1797. X        t->next= t_list;
  1798. X        t_list= t;
  1799. X        return(t_list->number);
  1800. X      }
  1801. X      else {
  1802. X        for (t= t_list; (t->next) && (strcmp(s,t->next->token) > 0); t= t->next);
  1803. X        u= t->next;
  1804. X        t->next= new_token(s);
  1805. X        t->next->next= u;
  1806. X        return(t->next->number);
  1807. X      }
  1808. X    }
  1809. X  }
  1810. X}
  1811. X
  1812. Xvoid print_token(tok)
  1813. Xint tok;
  1814. X{ TOKEN_LIST *t;
  1815. X
  1816. X  t= t_list;
  1817. X  while (t) {
  1818. X    if (t->number == tok) {
  1819. X      printf("%s",t->token);
  1820. X      return;
  1821. X    }
  1822. X    t= t->next;
  1823. X  }
  1824. X
  1825. X/*
  1826. X * if we get down this far, it's screwed.
  1827. X */
  1828. X
  1829. X  panic("print_token");
  1830. X}
  1831. X
  1832. Xint token_val(tok)
  1833. Xint tok;
  1834. X{ TOKEN_LIST *t;
  1835. X
  1836. X  t= t_list;
  1837. X  while (t) {
  1838. X    if (t->number == tok)
  1839. X      return(t->value);
  1840. X    t= t->next;
  1841. X  }
  1842. X  panic("token_val");
  1843. X}
  1844. END_OF_token.c
  1845. if test 3916 -ne `wc -c <token.c`; then
  1846.     echo shar: \"token.c\" unpacked with wrong size!
  1847. fi
  1848. # end of overwriting check
  1849. fi
  1850. echo shar: End of shell archive.
  1851. exit 0
  1852.